package appapi.service.impl;

import appapi.Global.BusinessException;
import appapi.Request.DismantReq;
import appapi.Request.OrganizaPageReq;
import appapi.Vo.AppVo.GetOrgListVo;
import appapi.Vo.GetDismantPageVo;
import appapi.dao.AnnexMapper;
import appapi.dao.EquipmentdismantMapper;
import appapi.dao.OrganizationMapper;
import appapi.dao.StaffMapper;
import appapi.dto.AnnexType;
import appapi.entity.Zhongjiao.AnnexDto;
import appapi.entity.Zhongjiao.EquipmentdismantDto;
import appapi.entity.app.AnnexEntity;
import appapi.entity.basic.UserInfo;
import appapi.service.IEquipmentdismantService;
import appapi.utils.JwtUtil;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author djq
 * @date 2023/5/18 9:36
 * @description:
 */
@Component
public class EquipmentdismantServiceImpl implements IEquipmentdismantService {
    @Resource
    private EquipmentdismantMapper equipmentdismantMapper;
    @Resource
    private AnnexMapper annexMapper;
    @Resource
    private StaffMapper staffMapper;
    @Resource
    OrganizationMapper organizationMapper;


    @Override
    public List<Map<String, Object>> SelectDismantList(Long equipId) {

        return equipmentdismantMapper.selectDismantList(equipId);
    }

    @Override
    public Map<String, Object> SelectDismantDetail(Long dismId) {
        List<AnnexDto> annexDtos = annexMapper.selectAllByRecId(AnnexType.AnnexTypeEnum.dismantl.name(), dismId);
        Map<String, Object> stringObjectMap = equipmentdismantMapper.selectDismantDtail(dismId);
        stringObjectMap.put("annexUrl",annexDtos.stream().map(AnnexDto::getAnnexUrl).collect(Collectors.toList()));
        return stringObjectMap;

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void InsertEquipmentdismant(EquipmentdismantDto dto) {
        dto.setCreateDate(LocalDateTime.now());
        dto.setUpdateTime(LocalDateTime.now());
        equipmentdismantMapper.insertSelective(dto);
        //有附件则新增附件
        if (dto.getAnnexUrl().size() > 0) {
            //String[] user = JwtUtil.getUserInfoByJwtToken(request);
            //暂搁置
            List<AnnexEntity> annexCollect = dto.getAnnexUrl().stream().map(annex -> {
                AnnexEntity entity = new AnnexEntity();
                entity.setCreateTime(LocalDateTime.now());
                entity.setUpdateTime(LocalDateTime.now());
                entity.setAnnexTitle("安拆");
                entity.setRecId(dto.getDismId());
                entity.setAnnexType(AnnexType.AnnexTypeEnum.dismantl.name());
                entity.setAnnexUrl(annex);
                entity.setCreateUser(dto.getCreateUser());
                return entity;
            }).collect(Collectors.toList());

            annexMapper.insertList(annexCollect);
        }

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void UpdateEquipmentdismant(EquipmentdismantDto dto) {
        equipmentdismantMapper.updateByPrimaryKeySelective(dto);
        List<Long> ids = new ArrayList<>();
        ids.add(dto.getDismId());
        annexMapper.deleteByRecId(AnnexType.AnnexTypeEnum.dismantl.name(),ids);
        //有附件则新增附件
        if (dto.getAnnexUrl().size() > 0) {
            //String[] user = JwtUtil.getUserInfoByJwtToken(request);
            //暂搁置
            List<AnnexEntity> annexCollect = dto.getAnnexUrl().stream().map(annex -> {
                AnnexEntity entity = new AnnexEntity();
                entity.setCreateTime(LocalDateTime.now());
                entity.setUpdateTime(LocalDateTime.now());
                entity.setAnnexTitle("安拆");
                entity.setRecId(dto.getDismId());
                entity.setAnnexType(AnnexType.AnnexTypeEnum.dismantl.name());
                entity.setAnnexUrl(annex);
                entity.setCreateUser(dto.getCreateUser());
                return entity;
            }).collect(Collectors.toList());

            annexMapper.insertList(annexCollect);
        }
    }

    @Override
    public int DelEquipmentdismant(List<Long> dismId) {
      return equipmentdismantMapper.deleteByIds(dismId);
    }

    @Override
    public List<GetDismantPageVo> SelectDismantPage(HttpServletRequest request, DismantReq req) {

        PageHelper.startPage(req.getCurrPage(), req.getPageSize());

        return equipmentdismantMapper.selectDismantPage(req);
    }

    @Override
    public List<GetDismantPageVo> SelectDismantPage(UserInfo userInfo, DismantReq dto) {

        if (dto.getOrgaPathIds()==null) {
            //拿到人员信息
            Map<String, Object> staffDto = staffMapper.selectStaffDetail(userInfo.getUserid());
            String orgaPathIds = staffDto.get("orgaPathIds").toString();
            dto.setOrgaPathIds(orgaPathIds);
        }
        PageHelper.startPage(dto.getCurrPage(), dto.getPageSize());

        return equipmentdismantMapper.selectDismantPage(dto);
    }

    @Override
    public List<GetDismantPageVo> getList(DismantReq req) {
        return equipmentdismantMapper.selectDismantPage(req);
    }
}
